﻿namespace WLFramework.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using WLFramework.Common;

    internal class DataMapper
    {
        const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

        private static Dictionary<Type, MethodInfo> getValueMethods = new Dictionary<Type, MethodInfo>();
        private static Type readerType = typeof(IDataReader);
        private static MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull");
        private static MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("GetValue");
        private static Dictionary<int, Delegate> GetRowMapperByReaderCache = new Dictionary<int, Delegate>();

        static DataMapper()
        {
            getValueMethods[Types.Boolean] = typeof(IDataRecord).GetMethod("GetBoolean");
            getValueMethods[Types.Byte] = typeof(IDataRecord).GetMethod("GetByte");
            getValueMethods[Types.SByte] = typeof(IDataRecord).GetMethod("GetByte");
            getValueMethods[Types.Char] = typeof(IDataRecord).GetMethod("GetChar");
            getValueMethods[Types.DateTime] = typeof(IDataRecord).GetMethod("GetDateTime");
            getValueMethods[Types.Decimal] = typeof(IDataRecord).GetMethod("GetDecimal");
            getValueMethods[Types.Double] = typeof(IDataRecord).GetMethod("GetDouble");
            getValueMethods[typeof(float)] = typeof(IDataRecord).GetMethod("GetFloat");
            getValueMethods[Types.Guid] = typeof(IDataRecord).GetMethod("GetGuid");
            getValueMethods[Types.Int16] = typeof(IDataRecord).GetMethod("GetInt16");
            getValueMethods[Types.Int32] = typeof(IDataRecord).GetMethod("GetInt32");
            getValueMethods[Types.Int64] = typeof(IDataRecord).GetMethod("GetInt64");
            getValueMethods[Types.String] = typeof(IDataRecord).GetMethod("GetString");
        }

        public static Dictionary<Type, MethodInfo> GetValueMethods
        {
            get { return DataMapper.getValueMethods; }
        }
    }
}
